<?php


namespace app\services\user;


use app\models\tech\TechnicianMoneyModel;
use app\models\user\DetailedIntegralModel;
use app\models\user\DetailedMoneyModel;
use app\models\user\TechnicianModel;
use app\models\user\UserLevelModel;
use app\models\user\UserModel;
use app\services\BaseService;
use app\services\statistic\StatisticService;
use think\facade\Db;

//用户积分
class DetailIntegralService extends BaseService
{

    const RECHARGE = 1;
    const ORDER = 2;
    const WRITE_OFF = 3;
    const ADMIN_ADD = 4;
    const SIGN_IN = 5;

    const RETURN = 6;

    const CONVERT_BALANCE = 7;


    const REDEMPTION = 101;
    const ADMIN_REDUCE = 102;

    const POINT_PAYMENT = 103;


    protected static $typeArr = [

        self::RECHARGE => '余额充值送积分',
        self::ORDER => '下单送积分',
        self::WRITE_OFF => '核销送积分',
        self::ADMIN_ADD => '管理员增加',
        self::SIGN_IN => '签到赠送积分',
        self::RETURN => '取消订单退回积分',
        self::CONVERT_BALANCE => '绿色积分转消费积分',



        //积分减少
        self::REDEMPTION => '积分兑换',
        self::ADMIN_REDUCE => '管理员扣除',
        self::POINT_PAYMENT => '下单支付积分',
    ];

    protected static function initModel()
    {
        return new DetailedIntegralModel();
    }

    public static function getTypeArr()
    {
        return self::$typeArr;
    }

    public static function getTypeText($key)
    {
        $arr = self::$typeArr;
        return isset($arr[$key]) ? $arr[$key] : '未知类型';
    }

    /**
     * 添加余额变更记录
     * @param $userId
     * @param $type
     * @param $amount
     * @param $relatedUid
     * @param $orderSn
     * @param $remark
     * @return bool
     * @throws \Exception
     */
    public static function create($userId, $type, $amount, $orderSn = '', $relatedUid = 0, $remark = '')
    {
        try {
            $userInfo = UserModel::find($userId);
            if (!$userInfo) {
                return true;
            }
            $before = $userInfo['integral'];
            if ($type < 100) {
                $after = $before + $amount;
                $userInfo->total_integral = $userInfo->total_integral + $amount;
                //累计积分升级
                $ids = self::upgrade($userInfo->total_integral,$userInfo['level_id']);
                if($ids){
                    $userInfo->level_id = $ids;
                }
            } else {
                if ($before < $amount) {
                    throw new \Exception('用户积分不足');
                }
                $after = $before - $amount;
            }
            $userInfo->integral = $after;
            $userInfo->save();
            return self::initModel()->save([
                'user_id' => $userId,
                'type' => $type,
                'before' => $before,
                'amount' => $amount,
                'after' => $after,
                'related_uid' => $relatedUid,
                'order_sn' => $orderSn,
                'remark' => $remark ? $remark : self::getTypeText($type),
            ]);
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }



    public static function upgrade($amount,$level_id){
        $id = UserLevelModel::where('amount','<',$amount)->order('amount','desc')->value('id');
        if($id != $level_id){
            return $id;
        }
        return false;
    }
}